package LOGIN;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentSystem {
    public static void maintest() {
        ArrayList<Student> list = new ArrayList<>();
        loop:
        while (true) {
            System.out.println("---------------欢迎来到wk学生管理系统-----------------");
            System.out.println("1:添加学生");
            System.out.println("2:删除学生");
            System.out.println("3:修改学生");
            System.out.println("4:查询学生");
            System.out.println("5:退出学生");
            System.out.println("请输入您的选择");
            Scanner scanner = new Scanner(System.in);
            String choice = scanner.next();
            switch (choice) {
                case "1" -> addStudent(list);
                case "2" -> deleteStudent(list);
                case "3" -> updateStudent(list);
                case "4" -> queryStudent(list);
                case "5" -> {
                    System.out.println("退出");
                    break loop;
                }
                //可以替换上一行代码
                //system.exit(0);
                default -> System.out.println("没有这个选项");

            }
        }


    }

    //添加学生
    public static void addStudent(ArrayList<Student> list) {
        Student student = new Student();
        Scanner scanner = new Scanner(System.in);
        while (true) {
        System.out.println("请输入学生id");
        int id = scanner.nextInt();
        boolean flag=containsStudent(list, id);
        if (flag) {
            System.out.println("id已存在，请重新录入");
        }else {
            student.setId(id);
            break;
        }

        }

        System.out.println("请输入学生姓名");
        String name = scanner.next();
        student.setName(name);
        System.out.println("请输入学生年龄");
        int age = scanner.nextInt();
        student.setAge(age);
        System.out.println("请输入学生的家庭住址");
        String address = scanner.next();
        student.setAddress(address);
        //把学生对象添加到集合中
        list.add(student);
        System.out.println("学生信息添加成功");
    }

    public static void deleteStudent(ArrayList<Student> list) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要删除的id");
        int id = scanner.nextInt();
        int index = indexOfStudent(list, id);
        if (index >=0) {
            list.remove(index);
            System.out.println("id为:"+id+"的学生删除成功");
        }else{
            System.out.println("id不存在，删除失败");
        }
    }

    public static void updateStudent(ArrayList<Student> list) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要修改的学生的id");
        int id = scanner.nextInt();
        int index = indexOfStudent(list, id);
        if (index ==-1) {
            System.out.println("要修改的id"+id+"不存在，请重新输入");
            return;
        }
        Student student = list.get(index);
        System.out.println("请输入要修改的学生姓名");
        String newName = scanner.next();
        student.setName(newName);
        System.out.println("请输入要修改的学生年龄");
        int newAge = scanner.nextInt();
        student.setAge(newAge);
        System.out.println("请输入要修改的学生家庭地址");
        String newAddress = scanner.next();
        student.setAddress(newAddress);
        System.out.println("学生信息修改成功");

    }

    public static void queryStudent(ArrayList<Student> list) {

        if (list.size() == 0) {
            return;
        }
        System.out.println("id\t姓名\t年龄\t家庭住址");
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            System.out.println(student.getId() + "\t" + student.getName() + "\t" + student.getAge() + "\t" + student.getAddress());

        }
    }

    //判断id在集合中是否存在
   public static boolean containsStudent(ArrayList<Student> list, int id) {
//       for (int i = 0; i < list.size(); i++) {
//           Student student = list.get(i);
//           int sid = student.getId();
//           if (sid == id) {
//               return true;
//
//           }
//
//
//       }
//       return false;
        return indexOfStudent(list, id)>=0;
   }
   public static int indexOfStudent(ArrayList<Student> list, int id) {
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            if (student.getId() == id) {
                return i;
            }
        }
        return -1;
   }
}